package com.xtourms.client.dep.product;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.rm.product.ProductDetail;
import com.xtourms.client.rm.product.ProductService;
import com.xtourms.client.rm.product.ProductServiceAsync;
import com.xtourms.client.ui.util.LocationWidget;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyLabel;
import com.xtourms.client.ui.util.MoneyTextBox;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.shared.field.ProductField;

public class ProductDep extends Composite {

	private static ProductDepUiBinder uiBinder = GWT
			.create(ProductDepUiBinder.class);

	interface ProductDepUiBinder extends UiBinder<Widget, ProductDep> {
	}

	private JSONArray result;
	
	public ProductDep() {
		initWidget(uiBinder.createAndBindUi(this));
		result = new JSONArray();
		initSearchPanel();
		initResultTableHeader();
	}

	private void initResultTableHeader() {
		int col = 0;
		resultTable.getCellFormatter().setWidth(0, col, "100px");
		resultTable.setText(0, col++, "推荐名称");
		resultTable.getCellFormatter().setWidth(0, col, "60px");
		resultTable.setText(0, col++, "城市");
		resultTable.setText(0, col++, "简介");
		resultTable.getCellFormatter().setWidth(0, col, "60px");
		resultTable.setText(0, col++, "售价");
		resultTable.getRowFormatter().addStyleName(0, "result_header");
	}

	private void fill(JSONArray result, String date){
		if(null != result && result.size() > 0){
			for(int index = 0; index < result.size(); index++){
				JSONObject ret = (JSONObject)result.get(index);
				if(0 == index){
					int col = 0;
					resultTable.getCellFormatter().setWidth(0, col, "100px");
					resultTable.setText(0, col++, "推荐名称");
					resultTable.getCellFormatter().setWidth(0, col, "60px");
					resultTable.setText(0, col++, "城市");
					resultTable.setText(0, col++, "简介");
					resultTable.getCellFormatter().setWidth(0, col, "60px");
					resultTable.setText(0, col++, "售价");
					resultTable.getRowFormatter().addStyleName(0, "result_header");
				}
				fillRow(index+1, ret);
			}
		}else{
			initResultTableHeader();
		}
	}
	
	class detailShow implements ClickHandler {
		
		private int index;
		
		public detailShow(int index){
			this.index = index;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			JSONObject ret = (JSONObject) result.get(index);
			ProductDetail detail = new ProductDetail(ret);
			detail.center();
		}
	}
	
	private void fillRow(int row, JSONObject ret) {
		int col = 0;
		String name = ((JSONString)ret.get(ProductField.PDT_NAME)).stringValue();
		Anchor hName = new Anchor(name);
		hName.addClickHandler(new detailShow(row-1));
		resultTable.setWidget(row, col++, hName);
		resultTable.setText(row, col++, ((JSONString)ret.get(ProductField.PDT_CITY_NAME)).stringValue());
		resultTable.setText(row, col++, ((JSONString)ret.get(ProductField.PDT_DES)).stringValue());
		resultTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)ret.get(ProductField.PDT_SP)).toString()));
		if(0 == row%2){
			resultTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			resultTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiField FlexTable resultTable;
	
	@UiField TextBox pdt_name;
	@UiField LocationWidget location;
	
	@UiField MoneyTextBox low_price;
	@UiField MoneyTextBox high_price;
	@UiField DateBox in_date;
	
	private void initSearchPanel() {
		in_date.setFormat(new MysqlDateFormat());
		in_date.setValue(new Date());
	}
	
	protected static ProductServiceAsync ps = GWT.create(ProductService.class);
	
	@UiHandler("search")
	void search(ClickEvent evt){
		JSONObject condition = new JSONObject();
		condition.put(ProductField.PDT_NAME, new JSONString(pdt_name.getValue()));
		condition.put(ProductField.PDT_CITY_ID, new JSONNumber(location.getSelectedCityId()));
		
		if(-1!=low_price.getMoney()){
			condition.put(ProductField.LOW_PRICE, new JSONNumber(low_price.getMoney()));
		}
		if(-1!=high_price.getMoney()){
			condition.put(ProductField.HIGH_PRICE, new JSONNumber(high_price.getMoney()));
		}
		//TODO 检查早于今天的日期
		final String date = in_date.getValue().toString();
		condition.put(ProductField.IN_DATE, new JSONString(in_date.getValue().toString()));		
		
		ps.queryProduct(condition.toString(), new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String rooms) {
				resultTable.removeAllRows();
				JSONArray retArray = (JSONArray) JSONParser.parse(rooms);
				result = retArray;
				fill(retArray, date);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("查询产品发生异常:"+caught.getMessage());
			}
		});
		return;
	}
	
	@UiHandler("reset")
	void reset(ClickEvent evt){
		pdt_name.setValue("");
		location.reset();
		low_price.setValue("");
		high_price.setValue("");
		in_date.setValue(new Date());
		return;
	}
}
